package com.exrobot.module.rbt.controller.app;

import cn.hutool.core.io.IoUtil;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.util.object.BeanUtils;
import com.exrobot.module.infra.controller.admin.file.vo.file.FileUploadReqVO;
import com.exrobot.module.infra.service.file.FileService;
import com.exrobot.module.rbt.controller.admin.robotappversion.vo.RobotAppVersionRespVO;
import com.exrobot.module.rbt.controller.admin.robotfiles.vo.RobotFilesSaveReqVO;
import com.exrobot.module.rbt.controller.admin.robotinfo.vo.RobotInfoPageReqVO;
import com.exrobot.module.rbt.controller.app.vo.RobotFileUploadReqVO;
import com.exrobot.module.rbt.dal.dataobject.robotinfo.RobotInfoDO;
import com.exrobot.module.rbt.enums.MqttTopicSubEnum;
import com.exrobot.module.rbt.service.robotappversion.RobotAppVersionService;
import com.exrobot.module.rbt.service.robotfiles.RobotFilesService;
import com.exrobot.module.rbt.service.robotinfo.RobotInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.json.JSONArray;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.exrobot.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 软件版本")
@RestController
@RequestMapping("/")
@Validated
public class AppController {

    @Resource
    private RobotAppVersionService appVersionService;

    @Resource
    private RobotFilesService robotFilesService;

    @Resource
    private FileService fileService;

    @Resource
    private RobotInfoService robotInfoService;


    @GetMapping("app/upgrade/check")
    @Operation(summary = "获取最新软件版本信息")
    @PermitAll
    public CommonResult<RobotAppVersionRespVO> getCanteenList() {
        return success(BeanUtils.toBean(appVersionService.getRobotAppLastVersion(),RobotAppVersionRespVO.class));
    }

    @PostMapping("robot/files/logcreate")
    @Operation(summary = "上传文件")
    @PermitAll
    public CommonResult<String> uploadFile(RobotFileUploadReqVO uploadReqVO) throws Exception {
        MultipartFile file = uploadReqVO.getFile();
        byte[] content = IoUtil.readBytes(file.getInputStream());
        String name = file.getOriginalFilename();
        String type = file.getContentType();
        long size =file.getSize();
        String url = fileService.createFile(content, name,"robot", type);
        RobotFilesSaveReqVO filesSaveReqVO = new RobotFilesSaveReqVO();
        filesSaveReqVO.setName(name);
        filesSaveReqVO.setType(type);
        filesSaveReqVO.setUrl(url);
        filesSaveReqVO.setSize(size);
        robotFilesService.createRobotFiles(filesSaveReqVO);
        return success(url);
    }

    @GetMapping("robot/list")
    @PermitAll
    public CommonResult<List<RobotInfoDO>> getRobotList() {
        RobotInfoPageReqVO reqVO = new RobotInfoPageReqVO();
        reqVO.setStatus(0);
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        return success(robotInfoService.getRobotInfoList(reqVO));
    }

}